package com.pox.guli.service.edu.controller.admin;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pox.guli.common.base.result.R;
import com.pox.guli.service.edu.entity.Teacher;
import com.pox.guli.service.edu.entity.vo.TeacherQueryVo;
import com.pox.guli.service.edu.feign.OssFeignService;
import com.pox.guli.service.edu.service.TeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author pox21s
 * @since 2022-06-01
 */

@Slf4j
//@CrossOrigin
@Api(description = "讲师管理")
@RestController
@RequestMapping("/admin/edu/teacher")
public class TeacherController {

    @Resource
    private TeacherService teacherService;
    @Resource
    private OssFeignService ossFeignService;

    @ApiOperation("讲师列表")
    @GetMapping("/list")
    public R getTeacherList() {

        List<Teacher> teachers = teacherService.list();
        return R.ok().data("items", teachers);
    }

    @ApiOperation("根据讲师Id删除讲师")
    @DeleteMapping("/removeTeacher/{id}")
    public R removeTeacherById(@ApiParam("删除的Id") @PathVariable("id") String id) {

        boolean removeResult = teacherService.removeById(id);

        if (removeResult) {
            return R.ok().message("删除" + id + "号讲师成功。");
        } else {
            return R.error().message("删除" + id + "号讲师失败！");
        }
    }

    @ApiOperation("讲师分页")
    @GetMapping("/list-test/{page}/{limit}")
    public R getTeacherListPage(@ApiParam("页数") @PathVariable("page") Long page,
                                @ApiParam("每页的个数") @PathVariable("limit") Long limit) {

        // 1.查询出结果
        Page<Teacher> pageParam = new Page<>(page, limit);
        Page<Teacher> teacherPage = teacherService.page(pageParam);
        // 2.获取结果中所需的数据
        List<Teacher> records = teacherPage.getRecords();
        long total = teacherPage.getTotal();

        return R.ok().data("total", total).data("rows", records);
    }

    @ApiOperation("分页查询或根据分页条件查询并分页")
    @GetMapping("/list/{page}/{limit}")
    public R getTeacherListPageByConditional(@ApiParam(value = "页数", required = true) @PathVariable("page") Long page,
                                             @ApiParam(value = "每页的个数", required = true) @PathVariable("limit") Long limit,
                                             @ApiParam("分页条件") TeacherQueryVo teacherQueryVo) {
        /* 如不能直接用 Mybatis 提供的查询语句查询，则自己编写业务代码，并实现 */
        IPage<Teacher> pageModel = teacherService.selectPage(page, limit, teacherQueryVo);

        List<Teacher> records = pageModel.getRecords();
        long total = pageModel.getTotal();

        return R.ok().data("total", total).data("rows", records);
    }

    @ApiOperation("新增讲师")
    @PostMapping("/save")
    public R saveTeacher(@ApiParam(value = "新增讲师对象", required = true) @RequestBody Teacher teacher) {

        boolean save = teacherService.save(teacher);

        if (save) {
            return R.ok().message("添加讲师成功。");
        } else {
            return R.error().message("添加讲师失败。");
        }
    }

    @ApiOperation("根据讲师Id更新讲师信息")
    @PutMapping("/update")
    public R updateTeacherById(@ApiParam(value = "修改的讲师", required = true) @RequestBody Teacher teacher) {

        boolean b = teacherService.updateById(teacher);

        if (b) {
            return R.ok().message("修改讲师数据成功。");
        } else {
            return R.error().message("修改讲师失败，请稍后再试。");
        }
    }

    @ApiOperation("根据讲师Id查询信息")
    @GetMapping("/get/{id}")
    public R getTeacherById(@ApiParam(value = "讲师Id", required = true) @PathVariable("id") String id) {

        Teacher teacher = teacherService.getById(id);

        if (teacher != null) {
            return R.ok().data("item", teacher);
        } else {
            return R.error().message("没有查询到该讲师。");
        }
    }

    @ApiOperation("根据id列表删除讲师")
    @DeleteMapping("/batch-remove")
    public R removeRows(
            @ApiParam(value = "讲师id列表", required = true)
            @RequestBody List<String> idList) {

        boolean result = teacherService.removeByIds(idList);
        if (result) {
            return R.ok().message("删除成功");
        } else {
            return R.error().message("数据不存在");
        }
    }

    @ApiOperation("根据左关键字查询讲师名列表")
    @GetMapping("list/name/{key}")
    public R selectNameListByKey(
            @ApiParam(value = "查询关键字", required = true)
            @PathVariable String key) {

        List<Map<String, Object>> nameList = teacherService.selectNameListByKey(key);

        return R.ok().data("nameList", nameList);
    }

    @ApiOperation("远程调用测试")
    @GetMapping("/feign-test")
    public R testFeign() {

        return ossFeignService.testFeign();
    }


    @ApiOperation(value = "根据ID删除文件", notes = "根据ID删除讲师，逻辑删除")
    @DeleteMapping("/remove/{id}")
    public R removeById(@ApiParam(value = "ID", required = true) @PathVariable("id") String id) {

        // 删除图片
        teacherService.removeAvatarById(id);
        // 删除讲师
        boolean result = teacherService.removeById(id);
        if (result) {
            return R.ok().message("删除成功");
        } else {
            return R.error().message("数据不存在");
        }
    }

    @ApiOperation(value = "Sentinel测试")
    @GetMapping("/sentinel-test")
    public R sentinelTest() {
        log.info("sentinel测试方法。");
        return R.ok().message("sentinel测试方法。");
    }
}